import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React from "react";
import classes from "./txnDetail.module.less";
import utilsFilter from "../../../utils/filters";
import classNames from "classnames";
import useServices from "../../../hooks/useRequest";
import { getTokenRecordInfo } from "../../../api/wallet";
import { useMount, useSafeState } from "ahooks";
import { useDispatch, useSelector } from "react-redux";
import useRouter from "../../../hooks/useRouter";
import { formateDate, matchTheme, getSpec, getMapFinalValue, getChainName, getChainBrowser } from "../../../utils";
import { POLLING_INTERVAL } from "../../../config";
import useCalculateAmount from "../../../hooks/useCalculateAmount";
import usePolling from "../../../hooks/usePolling";
import toast from "../../../compoents/toast";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../../local";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var delay = function(wait) {
    return new Promise(function(resolve) {
        return setTimeout(resolve, wait);
    });
};
function TxnDetail(props) {
    var query = useRouter().query;
    var _useSafeState = _sliced_to_array(useSafeState(_object_spread({}, props)), 2), state = _useSafeState[0], setState = _useSafeState[1];
    var dispatch = useDispatch();
    var userInfo = useSelector(function(s) {
        return {
            userInfo: s.app.userInfo
        };
    }).userInfo;
    var t = useTranslation().t;
    var toFormatEther = useCalculateAmount().toFormatEther;
    var theme = useSelector(function(st) {
        return st.app.theme;
    });
    var services = useServices({
        getTokenRecordInfo: getTokenRecordInfo
    });
    var fetchBillInfo = function() {
        var _ref = _async_to_generator(function() {
            var _ref, code, data, msg, /** 通知上层修改状态 */ _props_handlerChangeStatus, e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            2,
                            ,
                            3
                        ]);
                        return [
                            4,
                            services.getTokenRecordInfo(props.id)
                        ];
                    case 1:
                        _ref = _state.sent(), code = _ref.code, data = _ref.data, msg = _ref.msg;
                        if (code == 200 || data.status !== "active") {
                            ;
                            setState(data);
                            (_props_handlerChangeStatus = props.handlerChangeStatus) === null || _props_handlerChangeStatus === void 0 ? void 0 : _props_handlerChangeStatus.call(props, data);
                            cancel === null || cancel === void 0 ? void 0 : cancel();
                        } else if (code !== 200) {
                            toast.createToast({
                                message: msg
                            });
                            throw data;
                        }
                        return [
                            2,
                            data
                        ];
                    case 2:
                        e = _state.sent();
                        return [
                            3,
                            3
                        ];
                    case 3:
                        return [
                            2
                        ];
                }
            });
        });
        return function fetchBillInfo() {
            return _ref.apply(this, arguments);
        };
    }();
    var _usePolling = usePolling(fetchBillInfo, {
        pollingInterval: POLLING_INTERVAL,
        manual: true
    }), run = _usePolling.run, cancel = _usePolling.cancel;
    useMount(function() {
        if (props.status === "active") {
            run === null || run === void 0 ? void 0 : run();
        }
    });
    var amountNum = toFormatEther(state.amount || "0", {
        contract: state.contract,
        chainType: state.chainType
    });
    var gasNum = toFormatEther(state.gas || "0", {
        contract: "n/a",
        chainType: state.chainType
    });
    var gasUnit = getMapFinalValue([
        getChainName(state.chainType || "")
    ], {
        BSC: "BNB",
        ETHEREUM: "ETH",
        CONFLUX: "CFX"
    });
    return /*#__PURE__*/ _jsx("div", {
        className: classes.txn_detail,
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes.tnx_detail_wrap,
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: classes.detail,
                    children: /*#__PURE__*/ _jsxs("div", {
                        className: classes.obj,
                        children: [
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.address,
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        className: classes.avatar,
                                        children: /*#__PURE__*/ _jsx("img", {
                                            src: props.type === "out" && userInfo.avatarLink ? userInfo.avatarLink : matchTheme(theme, {
                                                light: require("../../../assets/icons/avatar.svg"),
                                                dark: require("../../../assets/icons/dark/avatar.svg")
                                            })
                                        })
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classes.address_wrap,
                                        children: [
                                            /*#__PURE__*/ _jsx("span", {
                                                children: t(LocalKeyMap.from)
                                            }),
                                            /*#__PURE__*/ _jsx("span", {
                                                children: utilsFilter.stringMethod(state.from || "")
                                            })
                                        ]
                                    })
                                ]
                            }),
                            /*#__PURE__*/ _jsx("img", {
                                className: classes.arrow,
                                src: matchTheme(theme, {
                                    light: require("../../../assets/icons/to.svg"),
                                    dark: require("../../../assets/icons/dark/to.svg")
                                })
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.address,
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        className: classes.avatar,
                                        children: /*#__PURE__*/ _jsx("img", {
                                            src: props.type === "in" && userInfo.avatarLink ? userInfo.avatarLink : matchTheme(theme, {
                                                light: require("../../../assets/icons/avatar.svg"),
                                                dark: require("../../../assets/icons/dark/avatar.svg")
                                            })
                                        })
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classes.address_wrap,
                                        children: [
                                            /*#__PURE__*/ _jsx("span", {
                                                children: t(LocalKeyMap.to)
                                            }),
                                            /*#__PURE__*/ _jsxs("span", {
                                                children: [
                                                    " ",
                                                    utilsFilter.stringMethod(props.to || "")
                                                ]
                                            })
                                        ]
                                    })
                                ]
                            })
                        ]
                    })
                }),
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.description,
                    children: [
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.desc,
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.sub,
                                    children: t(LocalKeyMap.status)
                                }),
                                /*#__PURE__*/ _jsx("span", {
                                    className: classNames(classes.strong, state.status === "success" ? classes.success : state.status === "active" ? classes.padding : classes.fail),
                                    children: state.status === "success" ? t(LocalKeyMap["txn.status.comfirmed"]) : state.status === "active" ? t(LocalKeyMap["txn.status.padding"]) : t(LocalKeyMap["txn.status.failed"])
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.desc,
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.sub,
                                    children: t(LocalKeyMap.date)
                                }),
                                /*#__PURE__*/ _jsx("span", {
                                    children: formateDate(state.transferTime || "", "YYYY-MM-DD HH:mm UTC+8")
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.desc,
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.sub,
                                    children: t(LocalKeyMap.tokenStandard)
                                }),
                                /*#__PURE__*/ _jsx("span", {
                                    children: getSpec(state.chainType || "", state.spec || "")
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.desc,
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.sub,
                                    children: t(LocalKeyMap.amount)
                                }),
                                /*#__PURE__*/ _jsxs("span", {
                                    children: [
                                        amountNum,
                                        " ",
                                        state.coinName
                                    ]
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.desc,
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.sub,
                                    children: t(LocalKeyMap.transferFee)
                                }),
                                /*#__PURE__*/ _jsxs("span", {
                                    children: [
                                        gasNum,
                                        " ",
                                        gasUnit
                                    ]
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.drive
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classNames(classes.desc, classes.total),
                            children: [
                                /*#__PURE__*/ _jsx("span", {
                                    className: classes.total_sub,
                                    children: "Total amount:"
                                }),
                                /*#__PURE__*/ _jsxs("span", {
                                    className: classes.total_amount,
                                    children: [
                                        state.chainType === state.coinName ? /*#__PURE__*/ _jsxs(_Fragment, {
                                            children: [
                                                utilsFilter.numFormat(utilsFilter.toNonExponential(Number(amountNum) + Number(gasNum))),
                                                /*#__PURE__*/ _jsx("span", {
                                                    className: classes.coinName,
                                                    children: state.coinName
                                                })
                                            ]
                                        }) : /*#__PURE__*/ _jsxs(_Fragment, {
                                            children: [
                                                amountNum,
                                                " ",
                                                state.coinName,
                                                " + ",
                                                gasNum,
                                                " ",
                                                gasUnit
                                            ]
                                        }),
                                        state.txHash && /*#__PURE__*/ _jsx("span", {
                                            className: classes.view,
                                            onClick: function() {
                                                // if (ETHEREUM.includes(state.chainType || '')) {
                                                //   window.open(`https://etherscan.io/tx/${state.txHash}`);
                                                // } else {
                                                //   window.open(
                                                //     `https://${isDevChain ? 'testnet.' : ''}bscscan.com/tx/${
                                                //       state.txHash
                                                //     }`,
                                                //   );
                                                // }
                                                state.txHash && window.open(getChainBrowser(state.chainType || "", state.txHash));
                                            },
                                            children: /*#__PURE__*/ _jsx("img", {
                                                src: require("../../../assets/icons/view.svg")
                                            })
                                        })
                                    ]
                                })
                            ]
                        }),
                        (query === null || query === void 0 ? void 0 : query.from) === "sendToken" && /*#__PURE__*/ _jsx("div", {
                            className: classes.btn,
                            onClick: function() {
                                dispatch.app.PUSH({
                                    path: "/home"
                                });
                            },
                            children: "Done"
                        })
                    ]
                })
            ]
        })
    });
}
export default TxnDetail;

 //# sourceMappingURL=index.js.map